package com.hdax.dmw.item.mapstructs;

import com.hdax.dmw.dtos.item.HotItemsDto;
import com.hdax.dmw.entity.base.DmImage;
import com.hdax.dmw.entity.item.DmItem;
import com.hdax.dmw.item.feign.BaseFeign;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Mapper(componentModel = "spring")
public abstract class HotItemsDtoMapper {

    @Autowired
    private BaseFeign baseFeign;

    @Mappings({
            @Mapping(target = "imgUrl",expression = "java(imgUrl(source.getId()))")
    })
    public abstract HotItemsDto toTarget(DmItem source);
    public abstract List<HotItemsDto> toTargetList(List<DmItem> sources);

    public String imgUrl(Long itemId){
        DmImage image = baseFeign.image(itemId, 2L, 1L);
        return image==null?"":image.getImgUrl();
    }
}
